package com.coy.web.controller;

import com.coy.web.common.dto.DataResult;
import com.coy.web.common.dto.PageResult;
import com.coy.web.common.exception.BizResultCode;
import com.coy.web.common.util.security.MD5;
import com.coy.web.dao.entity.UserInfo;
import com.coy.web.dao.service.IUserInfoService;
import com.coy.web.zax.feign.dto.UserInfoDTO;
import com.coy.web.zax.feign.dto.UserInfoQueryDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户 Controller
 *
 * @author chenck
 * @gen 2021-06-12 21:09:08
 */
@Slf4j
@RestController
@RequestMapping("/userInfo")
@Api(tags = {"用户相关"})
public class UserInfoController {

    @Autowired
    IUserInfoService userInfoService;

    @PostMapping("/add")
    @ApiOperation(value = "添加用户信息", httpMethod = "POST")
    public DataResult<Long> add(@RequestBody @Valid UserInfoDTO input) {
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(input, userInfo);
        userInfo.setPassword(MD5.encode(input.getPassword()));
        userInfoService.save(userInfo);
        return DataResult.succ(userInfo.getId());
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据ID修改用户信息", httpMethod = "POST")
    public DataResult updateById(@RequestBody @Valid UserInfoDTO input) {
        if (null == input.getId()) {
            return DataResult.error(BizResultCode.ERR_PARAM.getCode(), "请传入id");
        }
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(input, userInfo);
        boolean rslt = userInfoService.updateById(userInfo);
        if (!rslt) {
            return DataResult.error(BizResultCode.ERR_SYSTEM.getCode(), "修改失败");
        }
        return DataResult.succ();
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "根据ID删除用户信息", httpMethod = "GET")
    public DataResult deleteById(@ApiParam(value = "用户id", required = true) Long id) {
        if (null == id) {
            return DataResult.error(BizResultCode.ERR_PARAM.getCode(), "请传入id");
        }
        boolean rslt = userInfoService.removeById(id);
        if (!rslt) {
            return DataResult.error(BizResultCode.ERR_SYSTEM.getCode(), "删除失败");
        }
        return DataResult.succ();
    }

    @GetMapping("/getById")
    @ApiOperation(value = "根据ID查询用户信息", httpMethod = "GET")
    public DataResult<UserInfoDTO> getById(@ApiParam(value = "用户id", required = true) Long id) {
        if (null == id) {
            return DataResult.error(BizResultCode.ERR_PARAM.getCode(), "请传入id");
        }
        UserInfo userInfo = userInfoService.getById(id);
        if (null == userInfo) {
            return DataResult.succ();
        }
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        BeanUtils.copyProperties(userInfo, userInfoDTO);
        return DataResult.succ(userInfoDTO);
    }

    @PostMapping("/queryList")
    @ApiOperation(value = "查询用户列表", httpMethod = "POST")
    public PageResult<UserInfoDTO> queryList(@RequestBody @Valid UserInfoQueryDTO input) {
        return userInfoService.queryList(input);
    }
}